package org.jcut.controller;

import java.util.HashMap;

import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jcut.bean.Orderform;
import org.jcut.config.AlipayConfig;
import org.jcut.data.Data;
import org.jcut.service.IOrderService;
import org.jcut.service.PayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("/OrderForm")
public class OrderFormController {
	@Autowired
    private PayService payService;

	@Autowired
	private IOrderService service;
	@Autowired
	private Data data;
	/**
	 * @author 胡琪
	 * @param uId 用户id
	 * @param adress 收货地址
	 * @param cId 商品id
	 * @param cNum 购买商品数量
	 * @param oPrice 购买商品总金额
	 * @param express 快递类型
	 * @param payType 支付方式
	 * @throws Exception 
	 * 
	 * */
	@RequestMapping("/add")
	public void add(HttpServletRequest httpRequest , HttpServletResponse httpResponse) throws Exception {
		
		String form=null;
		//System.err.print(""+order);
	
			
			String uId=httpRequest.getParameter("uId");
			String adress=httpRequest.getParameter("adress");
			String cId=httpRequest.getParameter("cId");
			//System.out.println("同时购买两件商品"+cId.split(","));
			String cNum=httpRequest.getParameter("cNum");//商品总量
			String express=httpRequest.getParameter("express");
			String payType=httpRequest.getParameter("payType");
			String oPrice=httpRequest.getParameter("oPrice");//商品总价
			Orderform order=new Orderform();
			order.setAdress(adress);
			order.setuId(uId);
			order.setcId(cId);
			order.setMyNum(cNum);
			order.setExpress(express);
			order.setPayType(payType);
			order.setMyPrice(oPrice);
			//order.setTime(new Date());
			//order.setId(UUID.randomUUID().toString());
		    //生成一笔支付记录，支付完成时将支付状态改为成功
	        //body 代表订单描述 根据实际情况使用

	      
	        Map<String, String> PayMap = new HashMap<>();
	        
	        PayMap.put("out_trade_no",UUID.randomUUID().toString());
	        //PayMap.put("total_amount",String.valueOf(order.getoPrice()));
	        PayMap.put("uId",order.getuId());
	        PayMap.put("adress",order.getAdress());
	        PayMap.put("cId",order.getcId());
	        PayMap.put("cNum", String.valueOf(order.getcNum()));
	        PayMap.put("express", order.getExpress());
		    PayMap.put("payType", order.getPayType());
		    try {
		    	
				form = payService.PayMoney(PayMap,order);
				
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		   
		     httpResponse.setContentType("text/html;charset=" + AlipayConfig.CHARSET);
		        httpResponse.getWriter().write(form);
		        httpResponse.getWriter().flush();
		        httpResponse.getWriter().close();
	}
	@ResponseBody
	@RequestMapping("/selectByForm")
	public Object selectByForm(@RequestParam("uId")String uId,@RequestParam(value="pageNum",required = false)String pageNum,@RequestParam(value="pageSize",required = false) String pageSize) {
		
		if ((pageNum != null && !("".equals(pageNum))) && (pageSize != null && !("".equals(pageSize)))) {
			PageHelper.startPage(Integer.parseInt(pageNum), Integer.parseInt(pageSize));
		}
		PageInfo<Orderform> pageInfo = new PageInfo<>(service.selectByPrimaryKey(uId));
		data.setData(pageInfo);
		
		data.setMsg("查询完成");
		data.setStatus("1");
		return data;
	}
	@ResponseBody
	@RequestMapping("/updateDis")
	public Object updateDis(@RequestParam("dis")String dis,@RequestParam("id")String id) {
		Orderform orderform=new Orderform();
		orderform.setId(id);
		orderform.setDis(Integer.parseInt(dis));
		int i=service.updateDis(orderform);
		if(i>0) {
			data.setData(i);
			data.setMsg("修改完成");
			data.setStatus("1");
		}else {
			data.setData(i);
			data.setMsg("服务器异常");
			data.setStatus("0");
		}
		return data;
	}
	@ResponseBody
	@RequestMapping("/getFromCount")
	public Object getFromCount(String uId) {
		long i=service.getFromCount(uId);
		if(i>0) {
			data.setData(i);
			data.setMsg("查询成功");
			data.setStatus("1");
		}else {
			data.setData(i);
			data.setMsg("没查询到数据");
			data.setStatus("0");
		}
		return data;
	}
	/**
	 * @author 胡琪
	 * @param String uId 用户id
	 * */
	@ResponseBody
	@RequestMapping("/selectFormComment")
	public Object selectFormComment(@RequestParam("uId")String uId,@RequestParam(value="pageNum",required = false)String pageNum,@RequestParam(value="pageSize",required = false) String pageSize) {
		
		if ((pageNum != null && !("".equals(pageNum))) && (pageSize != null && !("".equals(pageSize)))) {
			PageHelper.startPage(Integer.parseInt(pageNum), Integer.parseInt(pageSize));
		}
		PageInfo<Orderform> pageInfo = new PageInfo<>(service.selectFormComment(uId));
		data.setData(pageInfo);
		data.setMsg("查询成功");
		data.setStatus("1");
		return data;
	}
	/**
	 * @author 胡琪
	 * @param pageNum 当前页
	 * @param pageSize 每页显示多少条
	 * 
	 * */
	@ResponseBody
	@RequestMapping("/selectAdmin")
	public Object selectAdmin(@RequestParam(value="pageNum",required = false)String pageNum,@RequestParam(value="pageSize",required = false) String pageSize) {
		if ((pageNum != null && !("".equals(pageNum))) && (pageSize != null && !("".equals(pageSize)))) {
			PageHelper.startPage(Integer.parseInt(pageNum), Integer.parseInt(pageSize));
		}
		PageInfo<Orderform> pageInfo = new PageInfo<>(service.selectAdmin());
		
		data.setData(pageInfo);
		data.setMsg("查询成功");
		data.setStatus("1");
		return data;
	}


}
